package com.yonyou.ucf.mdf.app.isvdxq.common;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzManager {
    private static String JOB_GROUP_NAME = "DxqJob01";
    private static String TRIGGER_GROUP_NAME = "trigger1";
    private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();

    /**
     * 添加一个定时任务，使用默认的任务组名，触发器名，触发器组名
     * @param jobName 任务名
     * @param cls 任务
     * @param time 时间设置
     */

    public static void addJob(String jobName, Class cls, String time) {
        try {
            // org.quartz-scheduler
            Scheduler sched = gSchedulerFactory.getScheduler();
            // 任务名，任务组，任务执行类
            //创建任务
            JobDetail jobDetail = JobBuilder.newJob(cls).withIdentity(jobName, TRIGGER_GROUP_NAME).build();
            //可以传递参数
            jobDetail.getJobDataMap().put("param", "railsboy");
            // 触发器
            //每秒钟触发一次任务
            CronTrigger trigger = TriggerBuilder.newTrigger().
                    withIdentity(jobName, TRIGGER_GROUP_NAME).
                    withSchedule(CronScheduleBuilder.cronSchedule("*/3 * * * * ? *")).
                    build();
            // 触发器时间设定
            sched.scheduleJob(jobDetail, trigger);
            // 启动
            if (!sched.isShutdown()) {
                sched.start();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //定时任务启动
    public static void main(String[] args) {
//        QuartzManager.addJob("DxqJob01",DxqJob01.class,"");
    }
}
